import json
import os

THROUGHPUT_PATH = '../data/Network_throughput/'
MULTI_THROUGHPUT_PATH = '../data_622/Network_throughput/'
FOV_PATH = '../data/FoV/'
MULTI_FOV_PATH = '../data_622/FoV/'


def load_throughput():
    """
    加载吞吐量信息，其中总吞吐量记录六个tile为一组（维度：总时间长度x6）
    :return: 时间长度(时间戳)、总吞吐量记录[[t1,t2,t3,t4,t5,t6],[t1',t2',t3',t4',t5',t6']...]、吞吐量记录名称
    """
    throughput_files = os.listdir(THROUGHPUT_PATH)
    all_total_time = []
    all_total_throughput = []
    all_total_record = []

    for file in throughput_files:
        file_path = THROUGHPUT_PATH + file
        total_time = []
        total_throughput = []
        with open(file_path, 'r') as load_file:
            for line in load_file:
                t = line.strip('\n').split(',')
                t[0] = float(t[0])
                for i in range(1, len(t)):
                    t[i] = float(t[i])
                total_time.append(t[0])
                total_throughput.append(t[-6:])
        all_total_time.append(total_time)
        all_total_throughput.append(total_throughput)
        all_total_record.append(file.strip('.txt'))

    return all_total_time, all_total_throughput, all_total_record


def load_multi_throughput():
    """
    加载6*2*2中吞吐量信息，其中总吞吐量记录24个tile为一组（维度：总时间长度x24）
    :return: 时间长度(时间戳)、总吞吐量记录[[t1,t2,t3,t4,t5,t6],[t1',t2',t3',t4',t5',t6']...]、吞吐量记录名称
    """
    throughput_files = os.listdir(MULTI_THROUGHPUT_PATH)
    all_total_time = []
    all_total_throughput = []
    all_total_record = []

    for file in throughput_files:
        file_path = MULTI_THROUGHPUT_PATH + file
        total_time = []
        total_throughput = []
        with open(file_path, 'r') as load_file:
            for line in load_file:
                t = line.strip('\n').split(',')
                t[0] = float(t[0])
                for i in range(1, len(t)):
                    t[i] = float(t[i])
                total_time.append(t[0])
                total_throughput.append(t[-24:])
        all_total_time.append(total_time)
        all_total_throughput.append(total_throughput)
        all_total_record.append(file.strip('.txt'))

    return all_total_time, all_total_throughput, all_total_record


# 读取json格式fov
# def load_fov_old():
#     files = os.listdir(FOV_PATH)
#     total_time = 0
#     total_FoV = []
#     for file in files:
#         with open(FOV_PATH + file, 'r') as f:
#             load_file = json.load(f)
#             for i in range(len(load_file)):
#                 tmp = [float(load_file[i]['x']), float(load_file[i]['y']), float(load_file[i]['z'])]
#                 total_time += 1
#                 total_FoV.append(tmp)
#     return total_time, total_FoV


def load_fov():
    """
    加载FoV信息
    :return: 时间长度(时间戳)、视野朝向[[x1,y1,z1],[x2,y2,z2]...]
    """
    files = os.listdir(FOV_PATH)
    all_total_time = []
    all_total_FoV = []
    for file in files:
        total_time = []
        total_FoV = []
        with open(FOV_PATH + file, 'r') as f:
            for line in f:
                t = line.strip('\n').split(',')
                for i in range(len(t)):
                    t[i] = float(t[i])
                total_time.append(t[0])
                total_FoV.append(t[-3:])
        all_total_time.append(total_time)
        all_total_FoV.append(total_FoV)
    return all_total_time, all_total_FoV


def load_multi_fov():
    """
    加载6*2*2场景中的FoV信息
    :return: 时间长度(时间戳)、视野朝向[[x1,y1,z1],[x2,y2,z2]...]
    """
    files = os.listdir(MULTI_FOV_PATH)
    all_total_time = []
    all_total_FoV = []
    for file in files:
        total_time = []
        total_FoV = []
        with open(MULTI_FOV_PATH + file, 'r') as f:
            for line in f:
                t = line.strip('\n').split(',')
                for i in range(len(t)):
                    t[i] = float(t[i])
                total_time.append(t[0])
                total_FoV.append(t[-3:])
        all_total_time.append(total_time)
        all_total_FoV.append(total_FoV)
    return all_total_time, all_total_FoV


def load_bitrates(movie):
    """
    加载视频比特率信息，从低到高
    :param movie:视频名称
    :return:视频的不同比特率等级，单位kbps
    """
    with open('../data/movie/' + movie + '.json', 'r') as file:
        load_movie = json.load(file)
    bitrate = load_movie['bitrate_bps']
    for i in range(len(bitrate)):
        bitrate[i] = bitrate[i] // 1000
    return bitrate


def load_segment_size(movie):
    """
    加载视频每个segment中每个tile的文件大小
    :param movie:视频名称
    :return:segment size matrix
    """
    with open('../data/movie/' + movie + '.json', 'r') as file:
        load_movie = json.load(file)
    return load_movie['segment_size_bytes']


def load_multi_segment_size(movie):
    """
    加载6*2*2视频中视频每个segment中每个tile的文件大小
    :param movie:视频名称
    :return:segment size matrix
    """
    with open('../data_622/movie/' + movie + '.json', 'r') as file:
        load_movie = json.load(file)
    return load_movie['segment_size_bytes']


def load_tiled_segment_size(movie, tile_id):
    """
    加载特定tile的每个segment的文件大小
    :param movie:
    :param tile_id:
    :return:
    """
    with open('../data/movie/' + movie + '.json', 'r') as file:
        load_movie = json.load(file)
    tmp = load_movie['segment_size_bytes']
    total = []
    for i in range(len(tmp)):
        total.append(tmp[i][tile_id])
    return total


def load_multi_tiled_segment_size(movie, tile_id):
    """
    加载6*2*2中特定tile的每个segment的文件大小
    :param movie:
    :param tile_id:
    :return:
    """
    with open('../data_622/movie/' + movie + '.json', 'r') as file:
        load_movie = json.load(file)
    tmp = load_movie['segment_size_bytes']
    face = tile_id // 4
    tile_in_face = tile_id % 4
    total = []
    for i in range(len(tmp)):
        total.append(tmp[i][face][tile_in_face])
    return total